/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.timesoft.common.mail;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;

/**
 *
 * @author sorasaks
 */
public class SendMailTLS {
    
    private static final Logger LOG = Logger.getLogger(SendMailTLS.class);

    private final String title;
    private final String content;

    public SendMailTLS(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public void send() {
        final String username = "pa_in_data@hotmail.com";
        final String password = "";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.live.com");
        props.put("mail.smtp.port", "465");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("pa_in_data@hotmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("pa.in.dara@gmail.com"));
            message.setSubject(title);
            message.setText(content);

            Transport.send(message);
        } catch (MessagingException e) {
            LOG.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

}
